package com.renwei.yunlong.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.pager.WorkAnnexPager;
import com.renwei.yunlong.pager.WorkDetailsPager;
import com.renwei.yunlong.pager.WorkHistoryPager;
import com.renwei.yunlong.pager.WorkRecordingPager;
import com.renwei.yunlong.pager.WorkRelatedPager;
import com.renwei.yunlong.utils.ACache;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 工单信息--页面
 * <p>
 * Created by Administrator on 2017/8/28.
 */

public class WorkInformationActivity extends AppCompatActivity implements View.OnClickListener {

    @Bind(R.id.tab_layout)
    TabLayout mTabLayout;
    @Bind(R.id.v_pager)
    ViewPager mViewPager;
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    private LayoutInflater mInflater;

    private List<Fragment> list;
    private String[] titles = {"详情", "附件", "相关", "记录", "历史"};

    WorkInformationAdapter adapter;
    private ACache mCache;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_work_information);
        ButterKnife.bind(this);
        mCache = ACache.get(this);

        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);

        initData();
    }

    private void initData() {
        ivBack.setVisibility(View.VISIBLE);
        tvTitle.setText("工单信息");
        ivBack.setOnClickListener(this);
        mInflater = LayoutInflater.from(this);

        //页面，数据源
        list = new ArrayList<>();
        list.add(new WorkDetailsPager());
        list.add(new WorkAnnexPager());
        list.add(new WorkRelatedPager());
        list.add(new WorkRecordingPager());
        list.add(new WorkHistoryPager());

        //ViewPager的适配器
        adapter = new WorkInformationAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(adapter);

        //绑定
        mTabLayout.setupWithViewPager(mViewPager);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;

            default:
                break;
        }
    }

    class WorkInformationAdapter extends FragmentStatePagerAdapter {

        public WorkInformationAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        //重写这个方法，将设置每个Tab的标题
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
}
